<?php

namespace app\weixin\controller;

//活动列表
class Activity extends Base{

    public function _initialize(){
        parent::_initialize();
    }

    public function index(){
        $map = [];
        $pagesize = config('paginate')['list_rows'];//每页数量
        $param=request()->param(); //获取url参数
        $lists = db('activity')->where($map)->order('id desc')->paginate($pagesize,false,array('query' => array_splice($param,1)));
        $this->assign("lists",$lists);
        $this->assign("page", $lists->render());
        return $this->fetch();
    }

    //添加活动
    public function add_activity(){

        if(request()->isPost()){
            $data = input('post.');
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['create_time'] = time();
            $res = db('activity')->insertGetId($data);
            return $this->success('操作成功');
        }
        
        return $this->fetch();
    }

    //修改活动
    public function edit_activity(){
        $id = input('id');
        if(request()->isPost()){
            $data = input('post.');
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $res = db('activity')->where(['id'=>$id])->update($data);
            return $this->success('操作成功');
        }
        $data = db('activity')->where(['id'=>$id])->find();
        $data['start_time'] = date('Y-m-d',$data['start_time']);
        $data['end_time'] = date('Y-m-d',$data['end_time']);
        $this->assign('data',$data);
        return $this->fetch('add_activity');
    }

    //删除
    public function del_activity(){
        $id = input('id');
        $res = db('activity')->where(['id'=>$id])->delete();
        return $this->success('操作成功');
    }

    //设置状态
    public function activity_state(){
        $id=input('id');
        $status=input('status');
        $res = db('activity')->where(['id'=>$id])->setField(['status'=>$status]);
        return $this->success('操作成功');
    }  

} 